
unit uSettings;

interface

uses
 {$IFNDEF NOT_USE_TNT}
  TntClasses
 {$ELSE}
  WideClasses
 {$ENDIF};

const
  C_VALUE_SEPARATOR: WideString = '=';

type
  TSettings = class
  private
    FOptions: {$IFNDEF NOT_USE_TNT}TTntStrings{$ELSE}TWStrings{$ENDIF};
  public
    constructor Create;
    destructor Destroy; override;
    procedure LoadFromFile(const FileName: WideString);
    procedure SaveToFile(const FileName: WideString);

    procedure SetString(const Key: WideString; const Value: WideString);
    function GetString(const Key: WideString; const DefValue: WideString): WideString;

    procedure SetInt(const Key: WideString; const Value: Integer);
    function GetInt(const Key: WideString; const DefValue: Integer): Integer;

    procedure SetFloat(const Key: WideString; const Value: Extended);
    function GetFloat(const Key: WideString; const DefValue: Extended): Extended;

    procedure SetBool(const Key: WideString; const Value: Boolean);
    function GetBool(const Key: WideString; const DefValue: Boolean): Boolean;

  end;

implementation

uses
  SysUtils,
 {$IFNDEF NOT_USE_TNT}
  TntSysUtils, TntWideStrings
 {$ELSE}
  WideSysUtils, WideStrings
 {$ENDIF};

{ TSettings }

constructor TSettings.Create;
begin
  FOptions := {$IFNDEF NOT_USE_TNT}TTntStringList{$ELSE}TWideStringList{$ENDIF}.Create;
end;

destructor TSettings.Destroy;
begin
  FOptions.Free;
  inherited;
end;

function TSettings.GetBool(const Key: WideString;
  const DefValue: Boolean): Boolean;
begin
  Result := StrToBoolDef(GetString(Key, BoolToStr(DefValue, True)), DefValue);
end;

function TSettings.GetFloat(const Key: WideString;
  const DefValue: Extended): Extended;
begin
  Result := StrToFloatDef(GetString(Key, FloatToStr(DefValue)), DefValue);
end;

function TSettings.GetInt(const Key: WideString;
  const DefValue: Integer): Integer;
begin
  Result := StrToIntDef(GetString(Key, IntToStr(DefValue)), DefValue);
end;

function TSettings.GetString(const Key, DefValue: WideString): WideString;
var
  i: Integer;
begin
  i := FOptions.IndexOfName(WideLowerCase(Key));
  if i <> -1 then
    Result := PWideChar(FOptions.ValueFromIndex[i])
  else
    Result := DefValue;
end;

procedure TSettings.LoadFromFile(const FileName: WideString);
begin
  FOptions.LoadFromFile(FileName);
end;

procedure TSettings.SaveToFile(const FileName: WideString);
begin
  FOptions.SaveToFile(FileName);
end;

procedure TSettings.SetBool(const Key: WideString; const Value: Boolean);
begin
  SetString(Key, BoolToStr(Value, True));
end;

procedure TSettings.SetFloat(const Key: WideString; const Value: Extended);
begin
  SetString(Key, FloatToStr(Value));
end;

procedure TSettings.SetInt(const Key: WideString; const Value: Integer);
begin
  SetString(Key, IntToStr(Value));
end;

procedure TSettings.SetString(const Key, Value: WideString);
var
  i: Integer;
begin
  i := FOptions.IndexOfName(WideLowerCase(Key));
  if i <> -1 then
    FOptions.ValueFromIndex[i] := Value
  else
    FOptions.Add(WideLowerCase(Key) + C_VALUE_SEPARATOR + Value);
end;

{ ************************************************************* }
initialization
  SetLength(TrueBoolStrs, 3);
  TrueBoolStrs[0] := DefaultTrueBoolStr;
  TrueBoolStrs[1] := 'Yes';
  TrueBoolStrs[2] := '1';
  SetLength(FalseBoolStrs, 3);
  FalseBoolStrs[0] := DefaultFalseBoolStr;
  FalseBoolStrs[1] := 'No';
  FalseBoolStrs[2] := '0';

end.
